<?php


namespace app\home\controller;


use app\home\model\WechatDepartment;
use app\home\model\WechatUser;
use app\home\model\WechatUserTag;

class User extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->anonymous();
        $this->jssdk();
        //判断用户状态
        $this->userid = session('userId');
        $res = WechatUserTag::where(['userid' => $this->userid])->find();
        if($res) {
            switch ($res['tagid']) {
                case 1: //总经理
                    $permission = 1;
                    break;
                case 2: //副总
                    $permission = 1;
                    break;
                case 3: //科室长
                    $permission = 2;
                    break;
                default: //普通员工
                    $permission = 3;
                    break;
            }
        }else {
            $permission = 3;
        }
        $this->permission = $permission;
    }
    /**
     *  个人中心
     */
    public function index () {
        $user = WechatUser::where(['userid' => $this->userid])->field('id,userid,name,department,header,avatar')->find();
        $user['header'] = $user['header'] ? $user['header'] : $user['avatar'];
        $depart = WechatDepartment::get(['id' => $user['department']]);
        $user['department_name'] = $depart['name'];
        $user['is_permission'] = $this->permission;
        $this->assign('user',json_encode($user));
        return $this->fetch();
    }

    /**
     * 用户信息详情
     */
    public function info() {
        if(IS_POST) {
            $data = input('post.'); //key,value
            $res = WechatUser::update([$data['key'] => $data['value']],['userid' => $this->userid]);
            if($res) {
                return $this->success("修改成功");
            }else {
                return $this->error("修改失败");
            }
        }else {
            $detail = WechatUser::where(['userid' => $this->userid])->find();
            $depart = WechatDepartment::get(['id' => $detail['department']]);
            $detail['department_name'] = $depart['name'];
            $this->assign('detail',json_encode($detail));
            return $this->fetch();
        }
    }
}
